<?php
class CompetitionsController extends CompetitionAppController {

	public $uses = array('CompetitionPost','Competition');
	
	public function beforeRender(){
		parent::beforeRender();
		
		Configure::write('competition_domain','http://seopress.opinet');
		
	}
	public function beforeFilter() {
		$this->Auth->allow('install');
		$this->Auth->allow('review');
		$this->Auth->allow('share');
	}
	
	public function install($id=null) {
		Configure::write('debug',0);
		$this->layout = 'button';
		
		$this->Competition->id = $id;
		if (!$this->Competition->exists()) {
			throw new NotFoundException(__d('competitions','Invalid competition'));
		}		
		
		$this->request->data['Competition']['id'] = $id;
	}
	
	public function review($id=null) {
		Configure::write('debug',0);
		$this->Competition->id = $id;
		
		if (!$this->Competition->exists()) {
			throw new NotFoundException(__d('competitions','Invalid competition'));
		}
		$this->layout = 'competition';
		if ($this->request->is('post')) {
			$this->CompetitionPost->create();
			if ($this->CompetitionPost->save($this->request->data)) {
				$this->Session->setFlash(__d('competitions','The competition post has been saved'));
				$this->redirect(array('action' => 'share',$this->CompetitionPost->id));
			} else {
				$this->Session->setFlash(__d('competitions','The competition post could not be saved. Please, try again.'));
			}
		}
		
		$this->request->data['CompetitionPost']['product_url'] = $this->referer();
		$this->request->data['CompetitionPost']['competition_id'] = $id;
		$competition = $this->Competition->read(null, $id);
		$this->set('competition',$competition);
	}
	
	public function share($id = null) {
		Configure::write('debug',0);
		$this->layout = 'competition';
		$this->CompetitionPost->id = $id;
		if (!$this->CompetitionPost->exists()) {
			throw new NotFoundException(__d('competitions','Invalid competition post'));
		}
		$this->request->data = $this->CompetitionPost->read(null, $id);	
			
	}
}